use std::env;
use std::sync::OnceLock;
use sqlx::SqlitePool;
use tracing::info;
use crate::base::ServerError;
use crate::db::SqlPool;


static POOLS: OnceLock<SqlPool> = OnceLock::new();


/// 初始化连接
pub async fn init_sqlite_pool() -> Result<(), ServerError> {
    dotenvy::dotenv().ok();
    let database_url = env::var("DATABASE_URL").unwrap();
    info!("建立数据连接: {}", database_url);
    let pool = SqlitePool::connect(&database_url)
        .await
        .map_err(|_| ServerError::Message("连接数据库错误".into()))?;
    POOLS
        .set(pool)
        .map_err(|_| ServerError::Message("重复设置数据库连接".to_string()))
}


/// 获取连接
pub fn get_conn() -> SqlPool {
    POOLS.get().unwrap().clone()
}
